<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="ac.il.technion.cs236369.hw5.core.servlets.projects.ProjectParameters"%>
<script type="text/javascript">
	function projectSubmit( e) {
		if ( !dijit.byId( "<%= ProjectParameters.PROJECT_NAME %>") ||
			 !dijit.byId( "<%= ProjectParameters.PROJECT_DESCRIPTION %>") ||
			 !dijit.byId( "<%= ProjectParameters.PROJECT_BUDGET%>") ||
			 dijit.byId( "<%= ProjectParameters.PROJECT_NAME %>").value.length == 0 ||
			 dijit.byId( "<%= ProjectParameters.PROJECT_DESCRIPTION %>").value.length == 0 ||
			 dijit.byId( "<%= ProjectParameters.PROJECT_BUDGET%>").value.length == 0 ) {
	        dijit.byId('toaster').setContent('All fields has to be filled.', 'fatal');
	        dijit.byId('toaster').show();							
			return;
		}
		dojo.xhrPost( {
			form : "projectForm",
			load : function( data, ioArgs) {
				if ( data == "ERROR") {
			        dijit.byId('toaster').setContent('Backend error. Could not insert user, please try later.', 'fatal');
			        dijit.byId('toaster').show();							
					return;
				}
		        if ( dojo.query( "input[name=projectOp]").attr("value") == "PROJECT_UPDATE") {
			        projectsStore.fetch( {
				        query : { name : dijit.byId( "projectDialog").getValue( "<%= ProjectParameters.PROJECT_NAME %>")},
				        onComplete : function (item) {
					        dijit.byId( "projectsGrid").store.setValues( item, dojo.mixin( dijit.byId( "projectDialog").getValues( ), {projectId : data}));
				        }
			        });
			        dijit.byId('toaster').setContent('Project was sucessefuly updated.', 'error');
			        dijit.byId('toaster').show();
		        } else {
			        dijit.byId( "projectsGrid").store.newItem( dojo.mixin( dijit.byId( "projectDialog").getValues( ), {projectId : data}));
			        dijit.byId('toaster').setContent('Project was sucessefuly added.', 'error');
			        dijit.byId('toaster').show();
		        }
			    dijit.byId( "projectDialog").destroyRecursive( );
			},
			error: function( ){
		        dijit.byId('toaster').setContent('Backend error. Could not insert project, please try later.', 'fatal');
		        dijit.byId('toaster').show();		
		        dijit.byId( "projectDialog").destroyRecursive( );					
			}
		});
		
	}

	function projectCancel( ) {
		dijit.byId( "projectDialog").destroyRecursive( );
	}
</script>
<form action="/FinalProject/app/projectManagement" name="projectForm" id="projectForm">
	<table>
		<tr>
			<td>
				<label>
					Project name
				</label>
			</td>
			<td>
				<input type="text" dojoType="dijit.form.ValidationTextBox" id="<%= ProjectParameters.PROJECT_NAME %>" name="<%= ProjectParameters.PROJECT_NAME %>" required="true" promptMessage="Enter project name." invalidMessage="Name is required." />
			</td>
		</tr>
		<tr>
			<td>
				<label>
					Project description
				</label>
			</td>
			<td>
				<input text="text" dojoType="dijit.form.ValidationTextBox" id=<%= ProjectParameters.PROJECT_DESCRIPTION %> name=<%= ProjectParameters.PROJECT_DESCRIPTION %> required="true" promptMessage="Enter project description." invalidMessage="Description is required." />
			</td>
		</tr>
		<tr>
			<td>
				<label>
					Budget
				</label>
			</td>
			<td>
				<input text="text" dojoType="dijit.form.ValidationTextBox" id="<%= ProjectParameters.PROJECT_BUDGET%>" name="<%= ProjectParameters.PROJECT_BUDGET%>" required="true" promptMessage="Enter project budget." invalidMessage="Budget is required." regExp="(\d)*.(\d)*"/>
			</td>
		</tr>
		<tr>
			<td align="right" colspan=2>
				<button type="button" dojoType="dijit.form.Button" id="dlgResetButton" onClick="projectCancel">Cancel</button>
				<button type="button" dojoType="dijit.form.Button" id="dlgSubmitButton" onClick="projectSubmit">Submit</button>
			</td>
		</tr>
	</table>
	<input type="hidden" name="projectOp" value="PROJECT_INSERT" />
</form>